// pages/subject/subject.js
const api = require("../../../utils/api.js");
const app = getApp().globalData;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    industryList:[],
    examTypeCode:0,
    examCode:"exam0",
    categoryId:1183,
    width:0,
    height:0
  },
  //获取用户位置
  getUserLocation:()=>{
    wx.getLocation({
     type: 'wgs84',
     success (res) {
       console.log('获取的位置信息，暂未有下一步',res);
     },
     fail(error){
      console.log('获取用户位置信息失败',error);
     }
    })
  },
  //获取设备信息
  getAdviceInfo:(that)=>{
    let windowAll = api.getAdviceInfo();
    wx.setStorageSync("WINDOWALL",windowAll);
    that.setData({
      width:windowAll.width,
      height:windowAll.height
    });
  },
  left_scroll:function(res){
    //使用scroll_view跳转到页面指定位置
    console.log(res.currentTarget.dataset.id)
    this.setData({
      examTypeCode:res.currentTarget.dataset.id,
      examCode: "exam" + res.currentTarget.dataset.id
    })
  },
  getCourseItem:function (res){
    //获取课程,考试信息封装并跳转到首页
    console.log(res)
    //封装课程信息
    var courseInfo = res.currentTarget.dataset.categoryinfo.courseList[0];
    var courseAll = res.currentTarget.dataset.categoryinfo.courseList;
    wx.setStorageSync('COURSEINFO', courseInfo);
    wx.setStorageSync('COURSEALL', courseAll);
    //封装考试信息
    var category = res.currentTarget.dataset.categoryinfo;
    var categoryInfo={
      industryId:category.industryId,
      shorttitle:category.shorttitle,
      id:category.id,
      iconurl:category.iconurl,
      title:category.title,
      isShowUnit:category.isShowUnit
    }
    wx.setStorageSync('CATEGORYINFO', categoryInfo);
    //跳转到首页
    wx.reLaunch({
      url: '../../../pages/index/index'
    })
  },
  subjectData:async function(that) {
    //获取所有考试类别
    var url = '/exam/findExamByCategory';
    var params = {
      from:app.from,
      isShowCourse:1,
      market:app.market
    };
    let res = await api.sendPostsSync(url,params);

    if(res){
      that.setData({
        industryList:res.industryList
      })
    }


  },
  subjectSelected:function(that){
    var categoryInfo=wx.getStorageSync('CATEGORYINFO');
    if(categoryInfo){
      that.setData({
        categoryId:categoryInfo.id
      });
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    //判断用户是否第一次登陆
    if(!wx.getStorageSync("ADVICEINFO")){
      //获取用户位置
      that.getUserLocation(that);
      //获取设备信息
      that.getAdviceInfo(that);
    }
    //获取页面数据
    that.subjectData(that);
    //显示已选择课程样式
    that.subjectSelected(that);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})